<script>
    var baseUrl = "<?php echo $baseUrl; ?>?t=1";
</script>

<div class="layui-row search-row" id="search-box">
    <div class="item">
        <div class="row" style="width: 330px">
            <div class="box">
                <span class="title">状态：</span>
                <div data-key="is_status" data-type="list" class="search-key list" >
                    <ul>
                        <li><span data-value="1">正常</span></li>
                        <li><span data-value="2">停用</span></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <div class="item">
        <div class="row" style="width: 330px">
            <div class="box">
                <span class="title">检票员</span>
                <div data-key="is_check" data-type="list" class="search-key list" >
                    <ul>
                        <li><span data-value="1">是</span></li>
                        <li><span data-value="2">否</span></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <div class="item">
        <div class="row" style="width: 360px;">
            <div class="box">
                <span class="title">关键字查询：</span>
                <div data-key="key" data-type="txt" class="search-key txt">
                    <input type="text" value="" placeholder="姓名"/>
                </div>
            </div>
        </div>
        <div class="row" style="width: 460px;">
            <div class="btn">
                <button class="layui-btn layui-btn-sm btn-query">查询</button>
                <?php if(Cm::$app->limitPost('/platform/sys/manager','save')){ ?>
                    <a href="javascript:void(0)" class="layui-btn layui-btn-primary layui-btn-sm" onclick="app.edit.add()">+ 新增管理员</a>
                <?php } ?>
            </div>
        </div>
    </div>
</div>
<table data-url="<?php echo $baseUrl; ?>?action=list"
   data-search="#search-box"
   data-page=""
   data-height="full-230"
   id="data-list"
   lay-filter='data-list' style="display: none;">
<thead>
<tr>
    <th width="120" field="username">姓名</th>
    <th width="130" field="account">登录账户</th>
    <th width="130" field="role_name">角色</th>
    <th width="80" align="center" templet="#tpl-check">检票员</th>
    <th width="180" align="center" field="last_login_ip">上次登录IP</th>
    <th width="180" align="center" field="last_login_time">上次登录时间</th>
    <th width="80" align="center" templet="#tpl-status">状态</th>
    <th width="180" align="center" field="addtime">注册时间</th>
    <th minWidth="200" toolbar="#tpl-cmd">操作</th>
</tr>
</thead>
</table>

<script type="text/html" id="tpl-status">
    {{# if(d.status==1){}}
    <span style="color: green;">正常</span>
    {{# }else{}}
    <span style="color: red;">停用</span>
    {{# }}}
</script>

<script type="text/html" id="tpl-check">
    {{# if(d.is_check==1){}}
    <span style="color: green;">是</span>
    {{# }else{}}
    <span style="color: red;">否</span>
    {{# }}}
</script>

<script type="text/html" id="tpl-cmd">
    <?php if(Cm::$app->limitPost('/platform/sys/manager','info')){ ?>
        <button onclick="app.edit.update(this)" class="layui-btn layui-btn-xs">编辑</button>
    <?php } ?>
    <?php if(Cm::$app->limitPost('/platform/sys/manager','loglist')){ ?>
        <button onclick="app.loglist({{d.id}})" class="layui-btn layui-btn-xs">操作日志</button>
    <?php } ?>
    <?php if(Cm::$app->limitPost('/platform/sys/manager','del')){ ?>
        <button onclick="app.del({{d.id}})" class="layui-btn layui-btn-danger layui-btn-xs">删除</button>
    <?php } ?>
    <span style="display: none;">{{JSON.stringify(d)}}</span>
</script>

<script type="text/html" id="tpl-edit">
    <div class="layui-card" style="box-shadow: none;">
        <div style="width: 1px;height: 1px;overflow: hidden;">
            <input type="text">
            <input type="password">
        </div>
        <div class="layui-card-header">{{id>0?'编辑账户':'新增账户'}}</div>
        <div class="layui-card-body">
            <form class="layui-form" action="">

                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-inline">
                        <select  name="role" id="role" lay-filter="parent" lay-search="">
                            <?php foreach ($roles as $r){ ?>
                                <option value="<?php echo $r['id']?>"><?php echo $r['name']?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" value="{{name}}" lay-verify="required"  autocomplete="off" placeholder="请输入姓名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">登录账号</label>
                    <div class="layui-input-inline">
                        <input type="text" value="{{account}}" name="account" lay-verify="required"  autocomplete="off" placeholder="请输入登录账户" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">登录密码</label>
                    <div class="layui-input-inline">
                        <input type="password" value="{{pwd}}" name="pwd" lay-verify="required|pwd"  autocomplete="off" placeholder="请输入登录密码" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="remark" class="layui-textarea" placeholder="备注信息" >{{remark}}</textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">是否检票</label>
                    <div class="layui-input-block">
                        <input type="checkbox" lay-filter="is_check" name="is_check" lay-skin="switch" lay-text="ON|OFF" {{is_check==1?'checked':''}}>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">启用状态</label>
                    <div class="layui-input-block">
                        <input type="checkbox" lay-filter="status" name="status" lay-skin="switch" lay-text="ON|OFF" {{status==1?'checked':''}}>
                    </div>
                </div>
                <?php if(Cm::$app->limitPost('/platform/sys/manager','save')){ ?>
                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="*">保存</button>
                    </div>
                </div>
                <?php } ?>
            </form>
        </div>
    </div>
</script>
<script type="text/html" id="tpl-loglist">
    <div style="padding: 5px;">
        <table data-url="<?php echo $baseUrl; ?>?action=loglist&id={{id}}"
           data-page=""
           data-height="full-230"
           id="data-loglist"
           lay-filter='data-list' style="display: none;">
        <thead>
        <tr>
            <th width="120" field="operator">管理员</th>
            <th width="130" field="typestr">操作类型</th>
            <th width="180" align="center" field="addtime">时间</th>
        </tr>
        </thead>
        </table>
    </div>
</script>
<script>
    var app = {
        init: function () {
            $('#data-list').table();
            this.form.init();
        },
        edit: {
            data:null,
            load: function () {

                layer.open({
                    title:false,
                    area:['430px','550px'],
                    type:1,
                    content:template('tpl-edit', this.data)
                });

                $("#role").val(this.data.role_id);
                app.form.render();
            },
            add: function () {
                this.data={
                    id:0,
                    store_id:'',
                    role_id:'',
                    name:'',
                    account:'',
                    pwd:'',
                    status:1,
                    is_check:0,
                    remark:''
                };
                this.load();
            },
            update: function (e) {
                var str=$(e).parent().find('span').html();
                var json=eval('('+str+')');

                this.data={
                    id:json.id,
                    store_id:json.store_id,
                    role_id:json.role_id,
                    name:json.username,
                    account:json.account,
                    pwd:json.pwd,
                    status:json.status,
                    is_check:json.is_check,
                    remark:json.remark
                };

                this.load();
            }
        },
        form: {
            obj: null,
            init: function () {
                var form = layui.form;
                form.render();

                form.verify({
                    pwd: [
                        /^[\S]{6,}$/
                        , '请填写6位以上的密码！'
                    ]
                });
                form.on('switch(status)', function (data) {
                    app.edit.data.status=data.elem.checked ? 1 : 0;
                });

                form.on('switch(is_check)', function (data) {
                    app.edit.data.is_check=data.elem.checked ? 1 : 0;
                });

                form.on('submit(*)', function (data) {
                    app.edit.data.name=data.field.name;
                    app.edit.data.pwd=data.field.pwd;
                    app.edit.data.account=data.field.account;
                    app.edit.data.store_id=data.field.store;
                    app.edit.data.role_id=data.field.role;
                    app.edit.data.remark = data.field.remark;
                    if(data.field.role == '' || data.field.role == 0){
                        layer.msg("请选择角色!", {icon: 5});
                        return false;
                    }
                    app.save();
                    return false;
                });

                this.obj = form;
            },
            render: function () {
                this.obj.render();
            }
        },
        save:function () {
            $.post(baseUrl+'&action=save',this.edit.data,function (d) {
                if(d.flag)
                {
                    layer.closeAll();
                    layer.msg(d.msg);
                    $('#data-list').table();
                }else{
                    layer.msg(d.msg);
                }
            },'json')
        },
        del:function (id) {
            layer.msg("确定删除该管理员吗？", {
                time: false, //20s后自动关闭
                btn: ['确定', '取消'],
                yes:function () {
                    $.post(baseUrl+'&action=del',{id:id},function (d) {
                        layer.closeAll();
                        if(d.flag)
                        {
                            layer.msg('删除成功');
                            $('#data-list').table();
                        }else{
                            layer.msg(d.msg);
                        }
                    },'json')
                }
            });

        },
        loglist:function(id){
            layer.open({
                title:false,
                area:['500px','80%'],
                type:1,
                content:template('tpl-loglist', {id:id})
            });
            $("#data-loglist").table();
        }
    };

    app.init();
</script>